package com.shoulder.boot.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shoulder.boot.system.dto.PostAddDTO;
import com.shoulder.boot.system.dto.PostModDTO;
import com.shoulder.boot.system.dto.PostQueryDTO;
import com.shoulder.boot.system.entity.Post;
import com.shoulder.boot.system.exception.SystemException;
import com.shoulder.boot.system.mapper.PostMapper;
import com.shoulder.boot.system.service.PostService;
import com.shoulder.core.fetch.BaseIds;
import com.shoulder.core.utils.TokenUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Optional;

/**
* 职位信息 服务实现类
*
* @author 596767880@qq.com
* @since 2024-03-14 14:13:55
*/
@Service
@Transactional
public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements PostService {


    @Override
    public List<Post> listQuery(PostQueryDTO queryDTO) {
        return this.baseMapper.selectList(buildQueryWrapper(queryDTO));
    }

    @Override
    public Post look(String id) {
        Post entity = this.baseMapper.selectById(id);
        if (ObjectUtils.isEmpty(entity)) {
            throw new SystemException("数据不存在");
        }
        return entity;
    }

    @Override
    public List<Post> looks(BaseIds<String> baseIds) {
        return this.baseMapper.selectBatchIds(baseIds.getIds());
    }

    @Override
    public void del(String id) {
        Post entity = this.baseMapper.selectById(id);
        if (ObjectUtils.isEmpty(entity)) {
            throw new SystemException("数据不存在");
        }
        this.baseMapper.deleteById(id);
    }

    @Override
    public void batchDel(BaseIds<String> baseIds) {
        this.baseMapper.deleteBatchIds(baseIds.getIds());
    }


    @Override
    public Page<Post> pageQuery(PostQueryDTO queryDTO) {

        return this.baseMapper.selectPage(new Page<>(queryDTO.getCurrent(), queryDTO.getSize())
            , buildQueryWrapper(queryDTO));
    }

    @Override
    public void mod(PostModDTO modDTO) {
        Post info = Optional.ofNullable(this.baseMapper.selectById(modDTO.getId())).orElseThrow(() -> new SystemException("职位不存在"));
        BeanUtils.copyProperties(modDTO, info);
        info.setTenantId(TokenUtils.parseUser().getTenantId());
        this.baseMapper.updateById(info);
    }

    @Override
    public void add(PostAddDTO addDTO) {
        Post info = this.baseMapper.selectOne(new LambdaQueryWrapper<Post>().eq(Post::getName, addDTO.getName()));
        if (!ObjectUtils.isEmpty(info)) {
            throw new SystemException("该职位已存在");
        }
        info = new Post();
        BeanUtils.copyProperties(addDTO, info);
        info.setTenantId(TokenUtils.parseUser().getTenantId());
        this.baseMapper.insert(info);
    }

    private LambdaQueryWrapper<Post> buildQueryWrapper(PostQueryDTO queryDTO) {
        LambdaQueryWrapper<Post> queryWrapper = new LambdaQueryWrapper<Post>().orderByDesc(Post::getCreateTime);
        queryWrapper.like(!ObjectUtils.isEmpty(queryDTO.getName()), Post::getName, queryDTO.getName());
        queryWrapper.like(!ObjectUtils.isEmpty(queryDTO.getTenantId()), Post::getTenantId, queryDTO.getTenantId());
        return queryWrapper;
    }
}
